# -*- coding: utf-8 -*-
# @Time    : 2019/11/11
# @Author  : CHENlj
# @File    : Assert.py

"""
封装Assert方法

"""
from Common.Log import Mylog


class Assert:

    def __init__(self):
        pass

    def assert_code(self, code, expected_code):
        """
        验证response状态码
        :param code:
        :param expected_code:
        :return:
        """
        try:
            assert code == expected_code
            return True
        except Exception:
            Mylog.error("statusCode error: expected_code is %s, statusCode is %s " % (expected_code, code))
            raise Exception('请求返回的状态码不正确！')

    def assert_code_in_list(self, code, expected_code_list):
        """
        验证response状态码
        :param code:
        :param expected_code_list: 列表对象
        :return:
        """
        try:
            assert code in expected_code_list
            return True
        except Exception:
            Mylog.error("statusCode error: expected_code is one of %s, statusCode is %s " % (expected_code_list, code))
            raise Exception('请求返回的状态码不正确！')

    def assert_body(self, body, body_msg, expected_msg):
        """
        验证response body中任意属性的值
        :param body:
        :param body_msg:
        :param expected_msg:
        :return:
        """
        try:
            msg = body[body_msg]
            assert msg == expected_msg
            return True
        except Exception:
            Mylog.error("Response body msg != expected_msg, expected_msg is %s, body_msg is %s" % (expected_msg, body_msg))
            raise Exception('返回信息和预期信息不一致')

    def assert_has_key(self, body, key):
        """
        验证健值
        :param body:
        :param key:
        :return:
        """
        try:
            assert body[key]
            return True
        except KeyError:
            Mylog.error('KeyError! The key value does not exist')
            raise KeyError('该健值不存在')

    def assert_in_text(self, text, inner_text):
        """
        验证text中是否包含字符串inner_text
        :param text:
        :param inner_text:
        :return:
        """
        try:
            assert str(inner_text) in str(text)
            return True
        except Exception:
            Mylog.error("text not contain inner_text, text is: %s, inner_text is: %s" % (text, inner_text))
            raise Exception('返回的信息不在预期结果之内')

    def assert_equal_text(self, text, expected_msg):
        """
        验证返回text是否等于预期字符串expected_msg
        :param text:
        :param expected_msg:
        :return:
        """
        try:
            assert str(text) == str(expected_msg)
            return True
        except Exception:
            Mylog.error("\n\nResponse text != expected_msg, \n\nexpected_msg is: \n%s \n\ntext is: \n%s" % (expected_msg, text))
            raise Exception('实际返回结果和预期结果不一致')

    def assert_time(self, response_time, expected_time):
        """
        验证response响应时间小于预期最大响应时间,单位：毫秒
        :param response_time:
        :param expected_time:
        :return:
        """
        try:
            assert response_time < expected_time
            return True

        except Exception:
            Mylog.error("Response time >= expected_time, expected_time is: %s, response_time is: %s" % (expected_time, response_time))
            raise Exception('应答响应时间大于预期响应时间')


if __name__ == "__main__":

    a = Assert()
    # a.assert_code(200,200)
    # a.assert_code(200,2000)
    dic = {'a': 1}
    a.assert_has_key(dic, 'b')
